<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深拷贝</title>
</head>

<body>
    <script>
        var obj = {
            a: 1,
            b: [1, 2, 3],
            c: {
                c1: 1
            }
        }

        function deepClone(obj) {
            //如果不是对象，直接返回
            if (typeof obj !== "object") return obj;
            let result;
            //判断是不是数组
            if (obj instanceof Array) {
                result = []
            }
            else {
                result = {}
            }
            for (const key in obj) {
                //判断key是不是自身的属性而不是原型的
                if (Object.hasOwnProperty.call(obj, key)) {
                    //递归
                    result[key] = deepClone(obj[key])
                }
            }
            return result
        }
        let obj1 = deepClone(obj);
        obj1.b.push(4);
        console.log(obj.b, obj1.b);//[1,2,3] [1,2,3,4]
    </script>
</body>

</html>